package com.snow.useviewdemo.longpicture;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.util.OnAntiDoubleClickListener;
import com.snow.useviewdemo.view.ObservableScrollView;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-09-20
 * desc   : 长图在iamgeview显示，可以上下滑动查看，外部嵌套ScrollView
 */
public class LoadLongPngScrollViewImgAct extends AppCompatActivity {
    ImageView imageView;
    ObservableScrollView nestedScrollView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_load_long_png_scrollview);
        imageView = findViewById(R.id.imageView);
        nestedScrollView = findViewById(R.id.nestedScrollView);

        findViewById(R.id.button01).setOnClickListener(new OnAntiDoubleClickListener() {
            @Override
            public void onAntiDoubleClick(View v) {
                //加载mipmap文件夹下的
                imageView.setBackgroundResource(R.mipmap.bg_big_png_02);

            }
        });
        findViewById(R.id.button02).setOnClickListener(new OnAntiDoubleClickListener() {
            @Override
            public void onAntiDoubleClick(View v) {
                loadOnLine();
            }
        });
        nestedScrollView.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(int x, int y, int oldx, int oldy, boolean isBottom) {
//                if (y - oldy > 10) {//向下滑动
//                    Log.e("snowzz", "==========向下滑动");
//                } else if (oldy - y > 10) {//向上滑动
//                    Log.e("snowzz", "==========向上滑动======");
//                }
            }

            @Override
            public void onScrollState(int scrollState) {
                Log.e("snowzz", "========================" + scrollState);
            }
        });
    }

    private void loadOnLine() {
        //加载线上图片，url
        String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1568978553140&di=0c8cada54d0cb46ccebe321f20b3b41d&imgtype=0&src=http%3A%2F%2Fwx2.sinaimg.cn%2Forj360%2F7dd58bb4gy1g2welv0sthj20u02tftlh.jpg";
        Glide.with(this)
                .asDrawable()//强制Glide返回一个Drawable对象
                .load(url)
                .into(new SimpleTarget<Drawable>() {

                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        imageView.setBackground(resource);
                    }
                });
    }
}
